package org.chen.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class MybatisPlusConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.old")
    public DataSource oldDataSource() {
        return new DruidDataSource();
    }

    @Bean
    public SqlSessionFactory oldSqlSessionFactory(@Qualifier("oldDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        return sqlSessionFactory.getObject();
    }

    @Configuration
    @MapperScan(basePackages = "org.chen.mapper.old", sqlSessionFactoryRef = "oldSqlSessionFactory")
    public class OldMapperConfig {
    }

    @Configuration
    @MapperScan(basePackages = "org.chen.mapper.news")
    public class NewMapperConfig {
    }
}